package com.ysstech.activiti.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysstech.activiti.model.GroupEntity;
import com.ysstech.activiti.service.ActivitiGroupService;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ActivitiGroupServiceImpl implements ActivitiGroupService {

    @Autowired
    private IdentityService identityService;

    /**
     * 查看所有的审核组信息
     *
     * @param page
     * @return
     */
    public Map<String, Object> queryListGroup(PageBean page) throws Exception {
        Map<String, Object> map = new HashMap<>();
        int pageNumber = Integer.parseInt(String.valueOf(page.getPageNum()));
        int pageSize = Integer.parseInt(String.valueOf(page.getPageSize()));
        List<Group> grouplist = identityService.createGroupQuery()
                .listPage((pageNumber - 1) * pageSize, pageSize);
        Long count = identityService.createGroupQuery().count();
        List<GroupEntity> groupEntityList = new ArrayList<>();
        for (Group group : grouplist) {
            GroupEntity groupEntity = new GroupEntity();
            groupEntity.setId(group.getId());
            groupEntity.setName(group.getName());
            groupEntity.setType(group.getType());
            groupEntityList.add(groupEntity);
            List<User> list = identityService.createUserQuery().memberOfGroup(group.getId()).list();
            List<com.ysstech.common.entity.User> userList = new ArrayList<>();
            for (User user : list) {
                com.ysstech.common.entity.User userentity = new com.ysstech.common.entity.User();
                userentity.setId(user.getId());
                userentity.setLoginId(user.getFirstName());
                userentity.setName(user.getLastName());
                userentity.setEmail(user.getEmail());
                userList.add(userentity);
            }
            groupEntity.setUserList(userList);
        }
        map.put("rows", groupEntityList);
        map.put("total", count);
        return map;
    }

    /**
     * 添加审核组信息
     *
     * @param groupId
     * @return
     * @throws Exception
     */
    public void addGroup(String groupId) throws Exception {
        Group group = identityService.createGroupQuery().groupId(groupId).singleResult();
        if (group == null) {
            group = identityService.newGroup(groupId);
        }
        group.setName(groupId);
        group.setType(groupId);
        identityService.saveGroup(group);
    }

    /**
     * 审核组里添加用户
     *
     * @param groupId
     * @param userId
     * @return
     * @throws Exception
     */
    public void addGroupUser(String groupId, String userId) throws Exception {
        User user = identityService.createUserQuery().userId(userId).singleResult();
        if (user == null) {
            user = identityService.newUser(userId);
        }
        user.setFirstName("yss2");
        user.setLastName("yss2");
        user.setEmail("123457@sina.com");
        user.setPassword("123457");
        identityService.saveUser(user);
        List<Group> groupInDb = identityService.createGroupQuery().groupMember(userId).list();
        for (Group group2 : groupInDb) {
            identityService.deleteMembership(userId, group2.getId());
        }
        identityService.createMembership(userId, groupId);
    }
}
